import { HumanCheckEnum, MoneyEnum } from "../配置/基础配置";
import { humanItemList, ItemLevelName, ItemPrice, ItemRecycleKey } from "../配置/装备等级";
import { CheckType } from "../配置/鉴定系统/鉴定系统";
import { checkItems } from "../附加功能/鉴定";

export interface GivePriceData {
    moneyType: MoneyEnum,
    price: number,
}

export class ItemMangae {
    //装备等级列表
    itemLevelList: Array<Array<string>>;
    //装备等级map
    itemLevelNameMap: any;
    //装备等级名称map
    itemLevelMap: any;
    //装备名称对应等级map
    itemMap: Map<string, number>;

    itemPriceList: Array<GivePriceData>;
    itemPriceMap: Map<string, GivePriceData>;

    constructor() {
        this.itemLevelList = humanItemList;
        this.itemLevelMap = {};
        this.itemLevelNameMap = {};
        this.itemMap = new Map<string, number>();
        for (let i = 0; i < ItemLevelName.length; i++) {
            let key = ItemLevelName[i];
            let list = humanItemList[i];

            this.itemLevelNameMap[key] = i;
            this.itemLevelMap[key] = list;

            for (let j = 0; j < list.length; j++) {
                let name = list[j]
                this.itemMap.set(name, i)
            }
        }

        this.itemPriceList = new Array<GivePriceData>();
        this.itemPriceMap = new Map<string, GivePriceData>();
        for (let i = 0; i < ItemPrice.length; i++) {
            let item = ItemPrice[i];
            let data: GivePriceData = item as GivePriceData;
            this.itemPriceList.push(data)
            this.itemPriceMap.set(this.getLvNameByIndex(i), data);

        }
    }

    //根据装备名称获取等级
    getLvIndexByItemName(name: string): number {
        if (this.itemMap.has(name)) {
            return this.itemMap.get(name);
        }
        return -1;
    }

    //根据等级名称获取列表
    getItemLvList(levelName: string): Array<string> {
        return this.itemLevelMap[levelName];
    }

    //根据装备等级序号获取列表
    getItemLvList2(levelIndex: number): Array<string> {
        return this.itemLevelList[levelIndex]
    }

    //获取所有等级装备的列表
    getAllLv(): Array<Array<string>> {
        return this.itemLevelList;
    }

    //根据等级名称获取等级序号
    getLvIndexByName(levelName: string): number {
        return this.itemLevelMap[levelName];
    }

    //根据等级序号获取等级名称
    getLvNameByIndex(index: number): string {
        return ItemLevelName[index];
    }

    //根据装备等级获取回收价格
    getPriceByLvIndex(index: number): GivePriceData {
        return this.itemPriceList[index];
    }

    //根据装备等级名称获取回收价格
    getPriceByLvName(name: string): GivePriceData {
        return this.itemLevelMap.get(name);
    }

    //根据装备名称获取回收价格
    getPriceByName(name: string): GivePriceData {
        let lvName = this.getLvIndexByName(name);
        return this.itemLevelMap.get(lvName);
    }


    getRecycleKey(index: number): HumanCheckEnum {
        return ItemRecycleKey[index];
    }

    onPickupItem(Player: TPlayObject, UserItem: TUserItem) {
        let lvIndex: number = this.getLvIndexByItemName(UserItem.Name);
        if (lvIndex >= 0) {
            let checkKey: HumanCheckEnum = this.getRecycleKey(lvIndex);
            //是否开启了自动回收
            if (Player.GetCheck(checkKey)) {
                let price: GivePriceData = this.getPriceByLvIndex(lvIndex);
                if (Player.CanAddGameMoney(price.moneyType, price.price)) {
                    Player.DeleteBagItem(UserItem, 1);//收走物品 
                    Player.AddGameMoney(price.moneyType, price.price, '回收')
                    Player.ReloadBag();
                    return;
                }
            }

            // this.giveItemAddValue(Player, UserItem);
            checkItems(UserItem, Player, CheckType.All, true)
        }
    }

    // //鉴定装备
    // giveItemAddValue(Player: TPlayObject, UserItem: TUserItem) {

    // }
}